package modelo;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import entities.MesaEntity;
import entities.MozoEntity;
import entities.SalonEntity;
import entities.SectorEntity;
import hbt.AbstractHibernateManager;

public class SalonDao extends AbstractHibernateManager {

	private static SalonDao instancia = null;
	
	public static SalonDao getInstancia()
	{
		if(instancia == null)
		{
			instancia = new SalonDao();
		}
		return instancia;
	}
	
	public void save(SalonEntity sal){
		Session s= null;
		try {
			s=getNewSession();
			s.beginTransaction();
			s.persist(sal);
			s.flush();
			s.getTransaction().commit();
			System.out.println("[salon PERSISTED ON TABLE salones]::OK");
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			closeSession(s);
		}
	}
	
	
	public void guardar(SalonEntity sal){
		Session s= null;
		try {
			s=getNewSession();
			s.beginTransaction();
			s.save(sal);
			s.flush();
			s.getTransaction().commit();
			System.out.println("[Salon SAVED ON TABLE salones]::OK");
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			closeSession(s);
		}
	}
	
	/**
	 * Retorna los datos de un salon en particular
	 * @param idSalon
	 * @return List<Object[]> con los datos del salon y sectores
	 */
	@SuppressWarnings("unchecked")
	public SalonEntity obtenerSalon(Integer idSalon) {
		Session s = null;
		SalonEntity salon = new SalonEntity();
		
		try {
			s = getNewSession();
			salon =  (SalonEntity) s.createQuery("FROM SalonEntity salon " +
									"WHERE salon.id = :idSalon")
							  .setInteger("idSalon", idSalon)
							  .uniqueResult();
			
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally{
			closeSession(s);
		}
		return salon;
	}
	
}
